import  type {MutationTree,ActionTree,GetterTree}  from 'vuex'
import  type  {State}  from '../index'
import { PlainObject } from "@/interface/base"; 
export interface RouterMoel extends PlainObject{
    name: string
    title: string
}
export  interface RoutersState<T>{
    nowRouter: T[],
  
}
let state:RoutersState<RouterMoel> = {
    nowRouter: []
};
//<RoutersState<>,State>//参数1是为了访问当前模块下的state的变量  参数2是引入的主模块下的

let getters:GetterTree<RoutersState<RouterMoel>,State> = {
    getNavRouters(newState){
        if (newState.nowRouter.length === 0){
            newState.nowRouter = JSON.parse( sessionStorage.boardMenu );
        }
        return newState.nowRouter;
    }
};
let mutations:MutationTree<RoutersState<RouterMoel>> = {
    SET_ROUTERS(newState, list) {
        newState.nowRouter = list;
    },
    UPDATE_ROUTERS(newState,router){
        newState.nowRouter.forEach(v => {
            if(v.name == router.name){
                v.title = router.title
                v.params = Object.assign(router.params)

            }
        })
    },
    DELETE_ROUTER(newState, name) {
        newState.nowRouter =  newState.nowRouter.filter(v => v.name != name)
    }
};
let actions:ActionTree<RoutersState<RouterMoel>,State> = {
    setRouters({commit}, list) {
        return commit('SET_ROUTERS', list)
    },
    updateRouters({commit},router){
        return commit('UPDATE_ROUTERS',router)
    },
    deleteRouter({commit},name){
        return commit('DELETE_ROUTER',name)
    }
};
export default {
  namespaced: true,//开始模块命名空间
  state,
  getters,
  mutations,
  actions,
};